package com.demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.Out;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		if(request.getParameter("password").equals("password") && request.getParameter("name").equals("admins")){

			PrintWriter prn=response.getWriter();

			ProductDetails product1=new ProductDetails();
			ProductDetails product2=new ProductDetails();
			ProductDetails product3=new ProductDetails();

			product1.setPrice(200);
			product1.setProductId(1);
			product1.setProductName("T-Shirt");
			product1.setQuantity(200);

			product2.setPrice(700);
			product2.setProductId(2);
			product2.setProductName("Jeans");
			product2.setQuantity(500);


			product3.setPrice(600);
			product3.setProductId(3);
			product3.setProductName("Shirt");
			product3.setQuantity(100);


			List<ProductDetails> list= new ArrayList<ProductDetails>();
			list.add(product1);
			list.add(product2);
			list.add(product3);

			session.setAttribute("productList", list);
			session.setAttribute("hacking", "<span>hii</span>");

			List<Integer> list2=new ArrayList<Integer>();
			list2.add(2);
			list2.add(3);
			list2.add(2);
			list2.add(3);
			
			list2.add(2);
			list2.add(3);
			list2.add(2);
			list2.add(3);
			
			getServletContext().setAttribute("intList", list2);
			response.sendRedirect("welcome.jsp");
		}
		else{

			session.setAttribute("errorMsg", "Please enter valid credential");
			response.sendRedirect("index.jsp");

		}	


	}

}
